var private const	PrintRot_pos = 1;
var private	rot_mat;
var private	center, org_vec;
var private	menu_input_flag = OFF;

function private PrintRot;

function private Input_rot()
{
	var	deg[3];

	DlogOpen( "夞揮妏擖椡", 5 );
	GetRotation( rot_mat, deg );
	DlogString( 0, "倃幉夞揮", substr(tostring( deg[0] ),6), 9 );
	DlogString( 2, "倄幉夞揮", substr(tostring( deg[1] ),6), 9 );
	DlogString( 4, "倅幉夞揮", substr(tostring( deg[2] ),6), 9 );
	if( DlogWait() )
	{
		deg[0] = atof( DlogAnswer( 0 ));
		deg[1] = atof( DlogAnswer( 2 ));
		deg[2] = atof( DlogAnswer( 4 ));
		rot_mat = rot( UNIT_MATRIX, AXISZ, deg[2] );
		rot_mat = rot( rot_mat, AXISY, deg[1] );
		rot_mat = rot( rot_mat, AXISX, deg[0] );
		menu_input_flag = ON;
		cur = center+(org_vec*rot_mat)*abs( Position(cur ) - Position(center));
		Cursor( cur );
		PrintRot();
	}
}

function private PrintRot()
{
	var	deg[3], i, temp, key;
	var	st[3], title;

	st = {"倃:", "倄:", "倅:" };
	GetRotation( rot_mat, deg );
	title = "";
	for( i = 0;i<3;i++ )
	{
		temp=tostring( deg[i] );
		key = search( temp, "." );
		temp = substr( temp, key );
		temp = substr( "   "+temp, -4 );
		title += " " + st[i] + temp;
	}
	Status_title[PrintRot_pos] = title;
	DrawStatus();
}

//
//夞揮乮flag == ON 偺偲偒楢摦偡傞揰傕堏摦乯
//
function _Rotate( rerationflag )
{
	var	org_pos[2], point[2], temp_point[2];
	var	i, j, k, key, ver;
	var	move_mat, mat;
	var	temp_vec;
	var	pick;
	var	vers, temp, temp2, temp3;
	var	mode = OFF, draw_flag = ON;
	var	edge, edge1, edge2, edge_temp;
	var	prev_sel, sel1, sel2;
	var	change_ver[MAXVERTEX], change;

	if ( SelectPolygons() == 0 )
	{
		Warning();
		if( novice_flag == ON )
			Message( error_msg3 );
		return ;
	}

	PushMenu();
	Status_org = Status_title;
	ClearStatus();
	if( rerationflag == ON )
	{
		MenuPosition( Menu( " 摦嶌愝掕(&S)",
				"夞揮妏擖椡",		Input_rot,
				"----------",		SEPARATE_MENU,
				"夞揮拞巭	ESC",	MenuQuit
			), Menu_Title );
		Status_title[0] = "亂慖戰柺傪夞揮拞亃";
	}
	else
	{
		MenuPosition( Menu( " 摦嶌愝掕(&S)",
				"夞揮妏擖椡",		Input_rot,
				"----------",		SEPARATE_MENU,
				"夞揮拞巭	ESC",	MenuQuit
			), Menu_Title );
		Status_title[0] = "亂夞揮拞亃";
	}
	quit_flag = FALSE;

	prev_sel = Select();
	SelectBox( org_pos );
//	愨懳抣偺戝偒偄曽偺揧帤偑 1

	if ( ShiftStat() & 1 )
		center = Cursor();
	else if( ShiftStat() & 2 )
		center = Vertex( 0, 0, 0 );
	else
		center = ( org_pos[0] + org_pos[1] )*scale( UNIT_MATRIX, 0.5, 0.5, 0.5 );

	temp_vec = Position( center );
	move_mat = move( UNIT_MATRIX, temp_vec*(-1));
	org_vec = vector( 1, 0, 0 );
	cur = center + org_vec*(ViewWidth(MouseWindow())/5);
	Cursor( cur );
	pick = cur;
	rot_mat = UNIT_MATRIX;
	ver = 1;
	PrintRot();

	if( rerationflag == ON )
	{
		sel1 = Select();
		edge1 = EdgeSelect();
		sel2 = SelectAdjoint( sel1 );
		sel2 = sel2 & ( !sel1 );
		Select( sel2 );
		edge2 = EdgeSelect();
		edge2 = edge2 - edge1;
		edge  = Edge();
		for( i = 0; i< Edges(edge2 );i++ )
		{
			EdgeGetVertex( edge2, i, point );
			Select( prev_sel );
			SelectArea( TRUE, SELECT_AND | SELECT_SUB, point[0], point[0] );
			if( SelectPolygons() != 0 )
			{
				edge_temp = Edge( point[0], point[1] );
				EdgeGetVertex( edge_temp, 0, temp_point );
				if( temp_point[0] == point[0] )
					edge = edge + edge_temp;
				else
				{
					edge = edge + edge_temp;
					edge = edge + edge_temp;
				}
			}
			else
			{
				Select( prev_sel );
				SelectArea( TRUE, SELECT_AND | SELECT_SUB, point[1], point[1] );
				if( SelectPolygons() != 0 )
				{
					edge_temp = Edge( point[0], point[1] );
					EdgeGetVertex( edge_temp, 0, temp_point );
					if( temp_point[0] == point[1] )
						edge = edge + edge_temp;
					else
					{
						edge = edge + edge_temp;
						edge = edge + edge_temp;
					}
				}
			}
		}
		j = Edges( edge );
	}

	Select( prev_sel );
	edge_temp = EdgeSelect();
	k = Edges( edge_temp );
	input_flag = OFF;
	change = 0;
	while( TRUE )
	{
		if( draw_flag == ON )
		{
			if( ver == 0 )
				DrawLine( center, cur );
			else
			{
				mat = move_mat * rot_mat / move_mat;
				DrawLine( center, cur );
				DrawLine( center, center+org_vec*(ViewWidth(MouseWindow())/8));
				if( mode == OFF )
					DrawBox( org_pos[0], org_pos[1], mat );
				else
				{
					DrawEdge( edge_temp*mat );
					if( rerationflag == ON )
					{
						for( i = 0; i< j; i++ )
						{
							EdgeGetVertex( edge, i, point );
							if( EdgeGetCount( edge, i ) == 1 )
								DrawLine( point[0]*mat, point[1] );
							else
								DrawLine( point[0], point[1]*mat );
						}
					}
				}
			}
		}
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if ( ShiftStat() & 1 )
		{
			cur = Cursor();
			TrimCursor( center, MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		key = KeyCode();
		if( input_flag | key == INPUT_KEY )
		{
			if( ver == 0 )
			{
				org_vec = unit(Position(cur) - Position(center));
				ver++;
			}
			else
				break;
		}
		else if( key == ESC | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			Select( prev_sel );
			return;
		}
		else if( key == BS )
		{
			if( ver == 0 )
			{
				PopandClear( OVERWRITE );
				Select( prev_sel );
				return;
			}
			else
				ver--;
		}
		else if( key == KEY_CLR )
			Input_rot();
		else if( key =='c' )
			mode = !mode;
		else
			temp = KeyProcess( key );
		if( pick != Cursor() & menu_input_flag == OFF )
			rot_mat = CalRotMatrix( org_vec, (Position( cur )- Position( center )));
		if( ver == 1 )
			PrintRot();
		if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
		{
			draw_flag = ON;
			Clear( OVERWRITE );
		}
		else
			draw_flag = OFF;
		menu_input_flag = OFF;
		input_flag = OFF;
		pick = Cursor();
	}
	Clear( OVERWRITE );
	if( pick != Cursor() & menu_input_flag == OFF )
		rot_mat = CalRotMatrix( org_vec, (Position( cur )- Position( center )));
	mat = move_mat * rot_mat / move_mat;
	if( rerationflag == ON )
	{
		Select(sel2 );
		k = SelectPolygons();
		for( i = 0; i< k; i++ )
		{
			PolyLoad( sel2, i );
			vers = PolyVertexs();
			change = 0;
			for( j = 0; j< vers; j++ )
			{
				temp = PolyGetVertex( j );
				Select( prev_sel );
				SelectArea( TRUE, SELECT_AND | SELECT_SUB, temp, temp );
				if( SelectPolygons() != 0 )
				{
					change_ver[change] = j;
					change++;
				}
			}
			PolyLoad( sel2, i );
			for( j = 0; j< change; j++ )
			{
				temp = PolyGetVertex( change_ver[j] );
				PolySetVertex( temp*mat, change_ver[j] );
			}
			PolySave();
		}
	}
	Select( prev_sel );
	PolyMove( mat );
	Update( CLEAR );
	UpdateObject();
	pers_rotation_flag = FALSE;
	PopMenu();
	Status_title = Status_org;
	DrawStatus();
}
